package com.iot.framework.voluntarily;

import com.iot.common.utils.RedisUtil;
import com.iot.system.domain.DeviceData;
import com.iot.system.service.IDeviceDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author mayanbin
 * @date 2024/8/21
 * @description TODO
 **/
@Component
public class saveMqttData {

    public static final String mqKey = "listQueue";
    private static final Logger log = LoggerFactory.getLogger(saveMqttData.class);

    @Resource
    private IDeviceDataService iDeviceDataService;

    @PostConstruct
    public void init() {
        // 在这里写入需要在启动时执行的代码
        //consume();
    }

    //这里消费redis里面 非阻塞的队列
    private void consume () {
        //判断是否开启监听
        boolean hasKey = RedisUtil.KeyOps.hasKey("mqtt:monitor:hasKey");
        while (true){
            if (hasKey){
                String content= RedisUtil.ListOps.lLeftPop(mqKey);
                log.info("saveMqttData_consume_打印消费的redis数据:{}",content);
                DeviceData deviceData = new DeviceData();
                iDeviceDataService.insertDeviceData(deviceData);
            }
        }
    }


}
